#include <iostream>
#include <brpc/server.h>
#include <butil/logging.h>
#include <brpc/channel.h>
#include <thread>
#include "main.pb.h"


void callback(brpc::Controller* cntl, ::example::EchoResponse* response)
{
    std::unique_ptr<brpc::Controller> cntl_guard(cntl);
    std::unique_ptr<example::EchoResponse> resp_guard(response);
    if(cntl->Failed())
    {
        std::cout << "rpc调用失败" << cntl->ErrorText() << std::endl;
        return;
    }
    std::cout << "收到响应: " << response->message() << std::endl;
}


int main(int args, char* argv[])
{
    //1,构造channel信道，连接服务器
    brpc::ChannelOptions options;
    options.connect_timeout_ms = -1; //连接等待超时时间，-1表示一直等待
    //2,构造EchoService_Stub对象，用于进行rpc调用
    options.timeout_ms = -1; //rpc请求等待超时时间， -1表示一直等待
    options.max_retry = 3; //请求重试次数
    options.protocol = "baidu_std"; //序列化协议，默认适应
    brpc::Channel channel;

    int ret = channel.Init("127.0.0.1:8080", &options);
    if(ret == -1)
    {
        std::cout << "初始化信道失败" << std::endl;
        return -1;
    }

    //3,构造EchoService_Stub对象，  用于进行rpc调用
    example::EchoService_Stub stub(&channel);
    example::EchoRequest req;
    req.set_message("你好 -- 哈哈");
    brpc::Controller* cntl = new brpc::Controller();
    example::EchoResponse* rsp = new example::EchoResponse();

    auto clusure = google::protobuf::NewCallback(callback, cntl, rsp);
    stub.Echo(cntl, &req, rsp, clusure); //发送请求，异步等待响应
    std::cout << "异步调用结束" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(3));
    return 0;
}



